<template>
  <div>
    <el-form
      :model="form"
      ref="form"
      label-width="120px"
      :rules="rules"
      class="form-container"
    >
      <el-form-item label="账号:" prop="username">
        <el-input v-model="form.username" placeholder="请输入账号"></el-input>
      </el-form-item>

      <el-form-item label="员工姓名:" prop="name">
        <el-input v-model="form.name" placeholder="请输入员工姓名"></el-input>
      </el-form-item>

      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>

      <el-form-item label="性别:" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="身份证号:" prop="idNumber">
        <el-input
          v-model="form.idNumber"
          placeholder="请输入身份证号"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <div class="bottom-buttons">
      <el-button type="default" @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleSave">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getEmployeeById, changeEmployee, addEmployee } from "@/api/employee";
export default {
  data() {
    return {
      form: {
        username: "",
        name: "",
        phone: "",
        sex: "1",
        idNumber: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        name: [{ required: true, message: "请输入员工姓名", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号 !",
            trigger: "blur",
          },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        idNumber: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
          {
            pattern: /^(?:\d{15}|\d{18}|\d{17}(\d|X|x))$/,
            message: "身份证号码不正确",
            trigger: "blur",
          },
        ],
      },
    };
  },
  async created() {
    if (this.isEdit) {
      const { data } = await getEmployeeById(this.$route.query.id);
      this.form = data;
    }
  },
  computed: {
    isEdit() {
      return this.$route.query.isEdit === "1";
    },
  },
  methods: {
    //取消按钮
    handleCancel() {
      this.$router.go(-1);
    },
    //保存按钮
    handleSave() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            await changeEmployee(this.form);
          } else {
            await addEmployee(this.form);
          }
          this.$router.push({
            name: "Employee",
          });
          this.$message.success("修改成功");
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style>
.form-container {
  width: 500px;
  padding: 20px;
  margin-left: 80px;
}

.bottom-buttons {
  display: flex;
  justify-content: center;
}
</style>
